package com.ruoyi.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class App {
    public static void main(String[] args) {

    }
    /**

     *执行命令

     */

    public boolean run_command(final String[] command, final File work_path) throws IOException, InterruptedException {

        List<String> result_list = new ArrayList<>();

        ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command);

        hiveProcessBuilder.directory(work_path);

        hiveProcessBuilder.redirectErrorStream(true);

        Process hiveProcess = hiveProcessBuilder.start();

        BufferedReader std_input = new BufferedReader(new InputStreamReader(hiveProcess.getInputStream(), "UTF-8"));

        BufferedReader std_error = new BufferedReader(new InputStreamReader(hiveProcess.getErrorStream(), "UTF-8"));

        String line;

        while ((line = std_input.readLine()) != null) {

            result_list.add(line);

        }

        while ((line = std_error.readLine()) != null) {
            log.error(line);
        }

        hiveProcess.waitFor();

        if (hiveProcess.exitValue() != 0) {

            log.error("failed to execute:" + command);

            return false;

        }

        log.info("execute success:" + command);

        IOUtils.closeQuietly(std_input);

        IOUtils.closeQuietly(std_error);

        return true;

    }

    /**

     *对命令进行处理

     */

    public static String[] partitionCommandLine(String command) {

        final ArrayList<String> commands = new ArrayList<>();

        int index = 0;

        StringBuffer buffer = new StringBuffer(command.length());

        boolean isApos = false;

        boolean isQuote = false;

        while (index < command.length()) {

            final char c = command.charAt(index);

            switch (c) {

                case ' ':

                    if (!isQuote && !isApos) {

                        final String arg = buffer.toString();

                        buffer = new StringBuffer(command.length() - index);

                        if (arg.length() > 0) {

                            commands.add(arg);

                        }

                    } else {

                        buffer.append(c);

                    }

                    break;

                case '\'':

                    if (!isQuote) {

                        isApos = !isApos;

                    } else {

                        buffer.append(c);

                    }

                    break;

                case '"':

                    if (!isApos) {

                        isQuote = !isQuote;

                    } else {

                        buffer.append(c);

                    }

                    break;

                default:

                    buffer.append(c);

            }

            index++;

        }

        if (buffer.length() > 0) {

            final String arg = buffer.toString();

            commands.add(arg);

        }

        return commands.toArray(new String[commands.size()]);

    }
}
